package com.ciis.system.authority.taglib;

import com.ciis.base.Constants;
import com.ciis.base.system.ControllerContext;
import com.ciis.system.authority.domain.TSysAuthority;
import com.ciis.system.user.domain.TSysUser;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-5-19
 * Time: 下午8:29
 * To change this template use File | Settings | File Templates.
 */
public class PermissionTag extends BodyTagSupport {

    private String code;

    public int doStartTag() throws JspException {
        HttpSession session = ControllerContext.getContext().getSession();
        HashMap<String,TSysAuthority> authMap =  (HashMap<String,TSysAuthority>)session.getAttribute(Constants.SESSION_USER_RIGHTS);

        TSysUser user = (TSysUser)session.getAttribute(Constants.SESSION_USER);
        // 如果为超级管理员，则不鉴权
        if(user != null && user.getSuperAdmin() == 1){
            return EVAL_BODY_INCLUDE;
        }

        if(code != null && authMap.containsKey(code)){
            return EVAL_BODY_INCLUDE;
        }
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException {
        return 6;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
